using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace CodingSmackdown.Services
{
    static public class PinManagement
    {
        static public OutputPort Digital0 = new OutputPort(Pins.GPIO_PIN_D0, false);
        static public OutputPort Digital1 = new OutputPort(Pins.GPIO_PIN_D1, false);
        static public OutputPort Digital2 = new OutputPort(Pins.GPIO_PIN_D2, false);
        static public OutputPort Digital3 = new OutputPort(Pins.GPIO_PIN_D3, false);
        static public OutputPort Digital4 = new OutputPort(Pins.GPIO_PIN_D4, false);
        static public OutputPort Digital5 = new OutputPort(Pins.GPIO_PIN_D5, false);
        static public OutputPort Digital6 = new OutputPort(Pins.GPIO_PIN_D6, false);
        static public OutputPort Digital7 = new OutputPort(Pins.GPIO_PIN_D7, false);
        static public OutputPort Digital8 = new OutputPort(Pins.GPIO_PIN_D8, false);
        static public OutputPort Digital9 = new OutputPort(Pins.GPIO_PIN_D9, false);
        static public OutputPort Digital10 = new OutputPort(Pins.GPIO_PIN_D10, false);
        static public OutputPort Digital11= new OutputPort(Pins.GPIO_PIN_D11, false);
        static public OutputPort Digital12 = new OutputPort(Pins.GPIO_PIN_D12, false);
        static public OutputPort Digital13 = new OutputPort(Pins.GPIO_PIN_D13, false);
        static public OutputPort OnboardLED = new OutputPort(Pins.ONBOARD_LED, false);

        static public SecretLabs.NETMF.Hardware.AnalogInput Analog0 = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A0);
        static public SecretLabs.NETMF.Hardware.AnalogInput Analog1 = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A1);
        static public SecretLabs.NETMF.Hardware.AnalogInput Analog2 = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A2);
        static public SecretLabs.NETMF.Hardware.AnalogInput Analog3 = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A3);
        static public SecretLabs.NETMF.Hardware.AnalogInput Analog4 = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A4);
        static public SecretLabs.NETMF.Hardware.AnalogInput Analog5 = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A5);

        static private OutputPort[] Digitals = new OutputPort[] { Digital0, Digital1, Digital2, Digital3, Digital4, Digital5, Digital6, Digital7, Digital8, Digital9, Digital10, Digital11, Digital12, Digital13 };
        static private SecretLabs.NETMF.Hardware.AnalogInput[] Analogs = new SecretLabs.NETMF.Hardware.AnalogInput[] { Analog0, Analog1, Analog2, Analog3, Analog4, Analog5 };

        static public void SwitchDigitalPinState(int PinNumber)
        {
            bool actState = GetDigitalPinState(PinNumber);

            Digitals[PinNumber].Write(!actState);
        }

        static public void SetDigitalPinState(int PinNumber, bool active)
        {
            Digitals[PinNumber].Write(active);
        }

        static public bool GetDigitalPinState(int PinNumber)
        {
            bool actState = true;

            actState = Digitals[PinNumber].Read();

            return actState;
        }

        static public int GetAnalogPinValue(int PinNumber)
        {
            int analogValue = 0;

            analogValue = Analogs[PinNumber].Read();

            return analogValue;
        }
    }
}
